
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Random;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.Image;
import java.awt.List;
 
 
public class Ball  {
 
		
		public int x=50,y=50,r=15,speedX=2,speedY=3;
		private Graphics g;
		private BallFrame bf ;
		private int random;
		public int blood;
		public Ball(int x, int y, int r, int speedX, int speedY, Graphics g, BallFrame bf,int blood) {
			super();
			this.x = x;
			this.y = y;
			this.r = r;
			this.speedX = speedX;
			this.speedY = speedY;
			this.g = g;
			this.bf = bf;
			this.blood = blood;
 
		}
		
		
		public void drawBall()
		{
			g.setColor(Color.green);
			g.fillOval(x-r, y-r, 2*r, 2*r);
			//Random ra = new Random();
			//random = ra.nextInt(8);
			//ImageIcon IMG = new ImageIcon("C:\\Users\\25280\\Desktop\\CSS之旅\\创萌网_项目创建界面3.0\\images\\subball"+random+".png");
			//Image img = IMG.getImage();
			//g.drawImage(img, x-r, y-r,null);
			
			
		}
		public void moveBall()
		{
			
			if(x+r>=800 )
			{
				speedX=-Math.abs(speedX);
				blood-=2;
				
			}
			else if(x<80)
			{
				speedX=Math.abs(speedX);
				blood-=2;
			}
			 if(y+r>=800)
			{
				speedY=-Math.abs(speedY);
				blood-=2;
			}
			 else if(y<80)
			 {
				 speedY=Math.abs(speedY);
				 blood-=2;
			 }
			x+=speedX;
			y+=speedY;
			System.out.println("调用moveBall方法,坐标、speex分别为：");
			System.out.println("x:"+x+" "+y+"speedX:"+speedX+" speedY:"+speedY);
		//	System.out.println("y:"+y);
			
		}
		
		public void clearBall()
		{
			g.setColor(Color.black);
			int xx = x-r-speedX;
			int yy = y-r-speedY;
			g.fillOval(xx,yy, 2*r, 2*r);
			
			//System.out.println("调用clearBall,擦去的坐标为：");
		//	System.out.println(xx+" "+yy);
			//System.out.println(y1-speedY );
			
		}
		
		public void isCrash(int i)
		{
			//判断是否发生了碰撞
					for(int j=0;j<bf.list.size();j++) {
						if(i==j) continue;//自己和自己不碰撞
						
						int DistanceX=x-bf.list.get(j).x;
						int DistanceY=y-bf.list.get(j).y;
						int RadiiI=r/2;
						int RadiiJ=bf.list.get(j).r/2;
						//两个小球之间的距离小于两者的半径和
						if((DistanceX*DistanceX+DistanceY*DistanceY)<=((RadiiI+RadiiJ)*(RadiiI+RadiiJ))) {
							if(x>bf.list.get(j).x) { //如果自己的x坐标大于发生碰撞的小球的x坐标，由数学知识可知应该朝正向运动
								speedX*=1;
							}
							else	//如果自己的x坐标小于发生碰撞的小球的x坐标，由数学知识可知应该朝负向运动
							{
								speedX*=-1;
							}
							if(y>bf.list.get(j).y) { //如果自己的x坐标大于发生碰撞的小球的x坐标，由数学知识可知应该朝正向运动
								speedY*=1;
							}
							else	//如果自己的x坐标小于发生碰撞的小球的x坐标，由数学知识可知应该朝负向运动
							{
								speedY*=-1;
							}
							blood--;
						}
						
					}
				}	
 
}